package com.gs.api.projects.shejiao.web;


import com.gs.api.core.Result;
import com.gs.api.projects.shejiao.entity.TMSaveNote;
import com.gs.api.projects.shejiao.service.ITMSaveNoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 存纸条表 前端控制器
 * </p>
 *
 * @author guoshao
 * @since 2021-10-09
 */
@Api(tags = "存纸条信息")
@RestController
@RequestMapping("{appid}")
public class TMSaveNoteController {

    @Autowired
    private ITMSaveNoteService itmSaveNoteService;

    @ApiOperation(value = "保存纸条", notes="保存纸条")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/save")
    public Result save(@PathVariable String appid,TMSaveNote saveNote){
        saveNote.setDelFlag(1);
        saveNote.setAppid(appid);
        itmSaveNoteService.save(saveNote);
        return Result.success();
    }

    @ApiOperation(value = "随机抽取纸条", notes="随机抽取纸条")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("random")
    public Result random(@PathVariable String appid,TMSaveNote saveNote){
        saveNote.setAppid(appid);
        TMSaveNote note = itmSaveNoteService.getRandSaveNotes(saveNote);
        if(null != note){
            note.setTakeNum(note.getTakeNum() + 1);
            itmSaveNoteService.updateById(note);
            itmSaveNoteService.insertTake(note.getId());
        }
        return Result.success(note);
    }

    /**
     *
     * select *,@rownum:=@rownum + 1 as rownum from
     * (
     * 	select * from t_m_save_note
     * 	ORDER BY id
     * ) a ,(SELECT @rownum:=0
     * ) t
     */


}
